<template>
  <a-modal :title="title" :width="600" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel" cancelText="关闭" destroyOnClose>
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name" required label="名称">
          <a-input placeholder="请输入" v-model="model.name" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attrCode" label="属性键">
          <a-input placeholder="请输入" v-model="model.attrCode" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code" required label="属性值">
          <a-input placeholder="请输入" v-model="model.code" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
// import pick from 'lodash.pick'
// import { addDict, editDict, duplicateCheck } from '@/api/api'
import { attrCreateOrUpdate } from '@/api/box/setting'
export default {
  name: 'DictModal',
  data() {
    return {
      value: 1,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: '请输入名称!' }],
        attrCode: [{ required: true, message: '请输入属性键!' }],
        code: [{ required: true, message: '请输入属性值!' }]
        // dictCode: [{ required: true, message: '请输入字典编码!' }, { validator: this.validateDictCode }]
      }
    }
  },
  created() {},
  methods: {
    /* validateDictCode(rule, value, callback) {
      // 重复校验
      var params = {
        tableName: 'sys_dict',
        fieldName: 'dict_code',
        fieldVal: value,
        dataId: this.model.id
      }
      duplicateCheck(params).then(res => {
        if (res.success) {
          callback()
        } else {
          callback(res.message)
        }
      })
    }, */
    handleChange(value) {
      this.model.status = value
    },
    add() {
      this.edit({})
    },
    edit(record) {
      if (record.id) {
        this.visiblekey = true
      } else {
        this.visiblekey = false
      }
      this.model = Object.assign({}, record)
      this.visible = true
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.model.name = (this.model.name || '').trim()
          this.model.attrCode = (this.model.attrCode || '').trim()
          this.model.code = (this.model.code || '').trim()
          let params = {
            requestData: this.model
          }
          attrCreateOrUpdate(params)
            .then(res => {
              if (res.code == 0) {
                that.$message.success('编辑成功！')
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

